# Create your views here.

import datetime
from django.shortcuts import render_to_response
from sentwitt.main import models 

from sentwitt.main import model_handling as mh 
import twitparser
import logging

def test_poll(request):
    tag = "#s"
    twitparser.TestSearch(tag)
    try:
        mh.ProcessSOVotes(datetime.date.today())
    except Exception,e:
        logging.error(e)

def poll(request):
    tag = "#Cnectd"
    twitparser.Search(tag)
    mh.ProcessSOVotes(datetime.date.today())

def main(request):
    winners=mh.GetWinners()
    loosers=mh.GetLoosers()
    return render_to_response("index.html",{'winners':winners,'loosers':loosers})

def search(request):
    if request.method == 'POST': # If the form has been submitted...
        form = SearchForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            so=models.GetSentObjectLog(form.cleaned_data['key'])
            if so:
                return render_to_response("base.html",{'so':so})
    else:
        return render_to_response('search.html')
    
